

package com.jiaai.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RestResultGenerator {
    private static final Logger LOGGER = LoggerFactory.getLogger(RestResultGenerator.class);

    public RestResultGenerator() {
    }

    public static <T> RestResult<T> genResult(int code, T data, String message) {
        RestResult<T> result = RestResult.newInstance();

        result.setCode(code);
        result.setData(data);
        result.setError(message);
        result.setMessage(message);

        if (data == null)result.setData((T) (message));
        if(LOGGER.isDebugEnabled()) {
            LOGGER.debug("generate rest result:{}", result);
        }

        return result;
    }

    public static <T> RestResult<T> genSuccessResult(T data) {
        return genResult(200, data, (String)null);
    }

    public static <T> RestResult<T> genSuccessResult(String message) {
        return genResult(200, (T)null, message);
    }

    public static <T> RestResult<T> genErrorResult(String message) {
        return genResult( 500, (T)null, message);
    }

    public static <T> RestResult<T> genErrorResult(int code, String message) {

        return genResult(code, (T)null, message);
    }





    public static RestResult genSuccessResult() {
        return genSuccessResult("操作成功");
    }
}
